package com.geeklan.smart.ui.network;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.databinding.ActivityDeviceNetworkOneBinding;
import com.geeklan.smart.ui.main.MainActivity;
import com.geeklan.smart.utils.AppUtils;
import com.geeklan.smart.utils.SharedPreferencesUtil;
import com.geeklan.smart.utils.WifiUtils;

public class DeviceNetworkOneActivity extends BaseActivity<ActivityDeviceNetworkOneBinding> {
    private boolean isPasswordVisible = false;
    @Override
    protected ActivityDeviceNetworkOneBinding initBinding() {
        return ActivityDeviceNetworkOneBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        // 检查权限并请求
        if (!hasPermissions()) {
            requestPermissions();
        } else {
            // 已有权限，执行你的逻辑
            getWifiSSID();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void initView() {
        mBinding.tvNext.setOnClickListener(v -> {
            if(!AppUtils.isWifiConnected(this)){
                Toast.makeText(this,"请连接WIFI",Toast.LENGTH_SHORT).show();
                return;
            }
            String ssid = mBinding.etInputWifiSsid.getText().toString();
            String pwd = mBinding.etInputWifiPwd.getText().toString();
            if (!ssid.isEmpty()) {
                DeviceNetworkTwoActivity.startActivity(this, ssid, pwd);
            }

            SharedPreferencesUtil.putString(ssid,pwd);
        });
        mBinding.tvSettingsWifi.setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityForResult(intent,100);
        });
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });

        mBinding.ivHidePwd.setOnClickListener(v -> {
            if (isPasswordVisible) {
                // 隐藏密码
                mBinding.etInputWifiPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mBinding.ivHidePwd.setImageResource(R.drawable.login_pwd_hint); // 隐藏图标
            } else {
                // 显示密码
                mBinding.etInputWifiPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mBinding.ivHidePwd.setImageResource(R.drawable.login_pwd_show); // 显示图标
            }
            isPasswordVisible = !isPasswordVisible;

            // 将光标移动到末尾
            mBinding.etInputWifiPwd.setSelection(mBinding.etInputWifiPwd.getText().length());
        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    public boolean isRegisterNetwork() {
        return true;
    }

    @Override
    public void networkStatus(boolean isConnected) {
        // 检查权限并请求
        if (!hasPermissions()) {
            requestPermissions();
        } else {
            // 已有权限，执行你的逻辑
            getWifiSSID();
        }
    }

    @Override
    public void networkType(int type) {

    }

    @Override
    public void onPermissionsResultSuccess() {
        super.onPermissionsResultSuccess();
        getWifiSSID();
    }

    private void getWifiSSID() {
        String ssid = WifiUtils.getConnectedSSID(this);
        mBinding.etInputWifiSsid.setText(ssid);
        mBinding.etInputWifiPwd.setText(SharedPreferencesUtil.getString(ssid,""));
    }

    @Override
    public void onPermissionsResultFail() {
        super.onPermissionsResultFail();
        Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_SHORT).show();
    }

    // 检查是否已有权限
    private boolean hasPermissions() {
        return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED;
    }

    // 请求权限
    private void requestPermissions() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_REQ_ID);
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, DeviceNetworkOneActivity.class);
        context.startActivity(intent);
    }
}